/**	
 * 
 * Copyright 2011 Chuan. All rights reserved.
 * 			 
 * Package: com.chuan.mantu 
 * FileName: PicShowActivity.java 
 * 
 * @version
 * @author javacyc@qq.com
 * @created 2012-11-15
 * @last Modified 
 * @history
 */

package com.chuan.mantu;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.chuan.mantu.abstracts.PicGesture;
import com.chuan.mantu.sql.DBManager;
import com.chuan.mantu.utils.ConnectionDetector;
import com.chuan.mantu.utils.Const;

import dalvik.system.VMRuntime;

/**
 * {图片内容展现}
 *  
 *  @author javacyc@qq.com
 *  @created 2012-11-15 下午9:47:03
 *  @lastModified       
 *  @history           
 */

public class PicShowActivity extends PicGesture{
	
	private ViewFlipper flipper;
	
	private ProgressBar progressBar;
	
	private TextView textView;
	
	private ConnectionDetector connectionDetector;
	private DBManager dbManager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		VMRuntime.getRuntime().setTargetHeapUtilization(0.75f);//手动干涉dalvik的内存处理效率
		setContentView(R.layout.picshow);
		textView = (TextView) this.findViewById(R.id.picshow_textview_tip);
		progressBar = (ProgressBar) this.findViewById(R.id.picshow_progressbar_loading);
		flipper = (ViewFlipper) this.findViewById(R.id.picshow_viewfilpper);
		super.initParams(flipper, progressBar);
		connectionDetector = new ConnectionDetector(this);
		dbManager = new DBManager(this);
		if(Const.VALID_INTENET){
			progressBar.setVisibility(View.VISIBLE);
			runTask();
		}else{
			textView.setVisibility(View.VISIBLE);
		}
		textView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//不使用2G/3G网络
				if(dbManager.getConfig(Const.CONFIG_INTENET_MOBILE).equals("0")){
					Const.VALID_INTENET = connectionDetector.isWifiConnection();
				}else{
					Const.VALID_INTENET = connectionDetector.isConnection();
				}
				if(Const.VALID_INTENET){
					textView.setVisibility(View.GONE);
					progressBar.setVisibility(View.VISIBLE);
					runTask();
				}else{
					progressBar.setVisibility(View.GONE);
					textView.setVisibility(View.VISIBLE);
				}
			}
		});
	}

	@Override
	protected void onPause() {
		if(Const.IF_BITMAP_RECYCLE){
			recycleBitmap(flipper);	
		}
		Const.IF_BITMAP_RECYCLE = false;
		super.onPause();
	}
	
	/**
	 * 
	 *  ｛回收不显示图片的内存｝
	 *  @param flipper
	 *  @author javacyc@qq.com
	 *  @created 2013-3-3 下午2:16:14
	 *  @lastModified       
	 *  @history
	 */
	private void recycleBitmap( ViewFlipper flipper){
	   if(flipper != null) {
         int count =  flipper.getChildCount();
         for(int i=0; i <count; i++ ){
	    	 ImageView img = (ImageView) flipper.getChildAt(i);
	          if(img != null){
	               Drawable drawable = img.getDrawable( );
	               if(drawable != null){
	                    if(drawable instanceof BitmapDrawable){
	                        BitmapDrawable bitmapDrawable = (BitmapDrawable)drawable;
	                        Bitmap bitmap = bitmapDrawable.getBitmap( );
	                        if(bitmap != null){
	                        	bitmap.recycle();
	                        	bitmap = null;
	                        	System.gc();
	                        }
	                    }
	               }
	           }
          }
	    }
	}

	@Override
	protected void onDestroy() {
		dbManager.closeDB();
		super.onDestroy();
	}
	
}